using System;
using AleaClasses;
using System.Collections.Generic;

namespace AleaPlayer
{
	public delegate void TAleaPlayerMessageDelegate(string aMessage);

	public class TAleaPlayer
	{
		public TAleaPlayer(TAleaGraph aAleaGraph)
		{
			fAleaGraph = aAleaGraph;
            _wavePlayer = new TWavePlayer<TNode>(SoundFinishedHandler);
		}

		private TAleaGraph fAleaGraph;
		private TAleaPlayerMessageDelegate fAleaPlayerMessageDelegate = null;
        private List<String> fDriveLabels = new List<String>();
        private TWavePlayer<TNode> _wavePlayer = null;

		public TAleaGraph AleaGraph
		{
			get
			{
				return fAleaGraph;
			}
		}

		public TAleaPlayerMessageDelegate AleaPlayerMessageDelegate
		{
			get
			{
				return fAleaPlayerMessageDelegate;
			}
			set
			{
				fAleaPlayerMessageDelegate = value;
			}
		}

		public void AddDriveLabel(string aDriveLabel)
		{
			fDriveLabels.Add(aDriveLabel);
		}

		public void ClearDriveLabels()
		{
			fDriveLabels.Clear();
		}

		public void DeleteDriveLabel(string aDriveLabel)
		{
			fDriveLabels.Remove(aDriveLabel);
		}

		public List<string> DriveLabels
		{
			get
			{
				return fDriveLabels;
			}
			set
			{
				fDriveLabels = value;
			}
		}

        private bool fStop = false;
        private bool fStopAfterTarget = false;

        public void StartPlaying()
        {
            TNode lnode;

            lnode = AleaGraph.StartNode;
            if ((lnode == null) && (AleaGraph.Nodes.Count > 0))
                lnode = AleaGraph.Nodes[0];

            fStop = false;
            fStopAfterTarget = false;
            PlayNode(lnode);
        }

        public void StopPlaying()
        {
            fStop = true;
            _wavePlayer.StopPlaying();
        }

        public void StopAfterReachingTarget()
        {
            fStopAfterTarget = true;
        }

        public bool Stopped
        {
            get
            {
                return !_wavePlayer.IsPlaying;
            }
        }

		private void SendMessage(string aMessage)
		{
			if (AleaPlayerMessageDelegate != null)
			{
				AleaPlayerMessageDelegate(DateTime.Now.ToString() + ": " + aMessage);
			}
		}

        private void PlayNode(TNode node)
        {
            if (node != null)
            {
                SendMessage("Node: " + node.DisplayName);
                SendMessage("Load and play the file " + node.FileName);
                _wavePlayer.StartPlaying(node.FileName, node);
            }
            else
            {
                SendMessage("Done");
            }
        }

        private void SoundFinishedHandler(TNode node)
        {
            SendMessage("Finished playing the file " + node.FileName);

            // Clearing Drive Labels
            if (node.ArrivalLabels != null)
            {
                foreach (TLabel lArrivalLabel in node.ArrivalLabels)
                {
                    if (fDriveLabels.Contains(lArrivalLabel.LabelCode))
                    {
                        SendMessage("Reached Label " + lArrivalLabel.DisplayName);
                        fDriveLabels.Remove(lArrivalLabel.LabelCode);
                    }
                }

                if (fStopAfterTarget && fDriveLabels.Count == 0)
                    StopPlaying();
            };

            if (!fStop)
            {
                // Now play next node
                TNode lnextNode = node.GetNextNode(fDriveLabels);

                PlayNode(lnextNode);
            }
        }
	}
}
